package tq.com.tqteacher;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.TextView;

import java.io.File;

import tq.com.easysurfacecapture.TQBitmapToVideoEncoder;
import tq.com.easysurfacecapture.TQCaputreView;
import tq.com.easysurfacecapture.TQOpencvRecord;
import tq.com.systools.system.FileHelper;
import tq.com.systools.system.PrivilageHelper;


public class MainActivity extends AppCompatActivity {
//    private TQBitmapToVideoEncoder videoEncoder = new TQBitmapToVideoEncoder(null);
    private TQCaputreView caputreView;
    private int REQUEST_CODE = 0x01;

    private PrivilageHelper.PermitionCheck storageCheck;
    private PrivilageHelper.PermitionCheck recordAudioCheck;

    private TQOpencvRecord opencvRecord;
    private String mp4File;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        caputreView = (TQCaputreView)findViewById(R.id.captureView);


        if(!PrivilageHelper.checkWritePermition(this)){
            String[] permition = {
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            };
            storageCheck = PrivilageHelper.requestPermition(this, permition, 0x01);
        }else{
            if(!PrivilageHelper.checkAudioPermition(this)){
                String[] permition = {Manifest.permission.RECORD_AUDIO};
                recordAudioCheck = PrivilageHelper.requestPermition(this, permition, 0x02);
            }
        }


        findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(opencvRecord.isRecording()){
                    opencvRecord.stopRecording();
                    ((TextView)v).setText("start");

                    gotoPlay();
                }else {
                    mp4File = FileHelper.fileWithSysUseTime("mp4");
                    opencvRecord.startRecording(mp4File);
                    ((TextView)v).setText("stop");
                }
            }
        });
        findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if(caputreView.getAction() == TQCaputreView.ACTION_SCROLL){
                caputreView.setAction(TQCaputreView.ACTION_PAINT_PATH);
                ((TextView)v).setText("to scroll");
            }else if(caputreView.getAction() == TQCaputreView.ACTION_PAINT_PATH){
                caputreView.setAction(TQCaputreView.ACTION_SCROLL);
                ((TextView)v).setText("to paint");
            }
            }
        });

        opencvRecord = new TQOpencvRecord();
        opencvRecord.initRecord(caputreView);
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(storageCheck != null && requestCode == storageCheck.getRequestCode()){
            storageCheck.checkResult(permissions, grantResults, new PrivilageHelper.PermitionResult() {
                @Override
                public void success(String permition) {
                    if(!PrivilageHelper.checkAudioPermition(MainActivity.this)){
                        String[] strpermition = {Manifest.permission.RECORD_AUDIO};
                        recordAudioCheck = PrivilageHelper.requestPermition(MainActivity.this, strpermition, 0x02);
                    }
                }

                @Override
                public void failed(String permition) {
                    if(!PrivilageHelper.checkAudioPermition(MainActivity.this)){
                        String[] strpermition = {Manifest.permission.RECORD_AUDIO};
                        recordAudioCheck = PrivilageHelper.requestPermition(MainActivity.this, strpermition, 0x02);
                    }
                }
            });
        }

        if(recordAudioCheck != null && recordAudioCheck.getRequestCode() == requestCode ){
            recordAudioCheck.checkResult(permissions, grantResults, new PrivilageHelper.PermitionResult() {
                @Override
                public void success(String permition) {

                }

                @Override
                public void failed(String permition) {

                }
            });
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void gotoPlay(){
        Intent intent = new Intent(this, PlayActivity.class);
        intent.putExtra("url", mp4File);
        startActivity(intent);
    }
}
